<?php
namespace app\office\controller;

use think\Request;
use think\Db;
use app\office\controller\OfficeBase;
use app\office\model\Organization as OrganizationModel;
use app\office\model\Position as PositionModel;

class OrganizationPosition extends OfficeBase
{
    protected $organization_filter=[];
    protected $position_filter=[];
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
      //验证Cookie↓
      $this->checkCookie();
      // 引入navbar
      $this->setNavbar();
      $this->assign('title',"组织机构-职位工程师列表");
      if(!input('?request.')){
        $data=input('request.');
        foreach($data as $k=>$v){
          if($v!=null& in_array($k,['organization_is_sth','organization_sth_num','organization_static'])){     /* 使用in_array($k,['is_sth','sth_num','static'],是因为paginate传参是get方法,需要筛选变量 */
            $k=str_replace("organization_","",$k);
            $this->organization_filter[$k]=$v;
          }
        }
      }
      
      // $organization_filter_msg=$this->organization_filter;
      $organization=OrganizationModel::scope('static')->where($this->organization_filter)->order('id','desc')->select();   /* 这里用的是查询构建器的select()方法;或者可以使用模型的all()方法,$list=OrganizationModel::all(); */
      foreach($organization as $k=>$v){
        $organization_list[$v->id]=$v;
        $organization_list[$v->id]['position']=$v->position;
      }
      $countall=OrganizationModel::scope('static')->where($this->organization_filter)->count();
      $this->assign('organization_list',$organization_list);
      $this->assign('countall',$countall);
      return $this->fetch();
    }
    
    /**
     * 显示资源列表(职位工程师为主体)
     *
     * @return \think\Response
     */
    public function index_position()
    {
      //验证Cookie↓
      $this->checkCookie();
      // 引入navbar
      $this->setNavbar();
      $this->assign('title',"职务-组织机构列表");        
      if(!input('?request.')){
        $data=input('request.');
        foreach($data as $k=>$v){
          if($v!=null& in_array($k,['position_is_sth','position_sth_num','position_static'])){     /* 使用in_array($k,['is_sth','sth_num','static'],是因为paginate传参是get方法,需要筛选变量 */
            $k=str_replace("position_","",$k);
            $this->position_filter[$k]=$v;
          }
        }
      }
      // $organization_filter_msg=$this->organization_filter;
      $position=PositionModel::scope('static')->where($this->position_filter)->order('id','desc')->select();   /* 这里用的是查询构建器的select()方法;或者可以使用模型的all()方法,$list=OrganizationModel::all(); */
      foreach($position as $k=>$v){
        $position_list[$v->id]=$v;
        $position_list[$v->id]['position']=$v->organization;
      }
      $countall=PositionModel::scope('static')->where($this->position_filter)->count();
      $this->assign('position_list',$position_list);
      $this->assign('countall',$countall);
      return $this->fetch();
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
      //验证Cookie↓
      $this->checkCookie();
      // 引入navbar
      $this->setNavbar();
      $this->assign('title',"组织机构-职位工程师分配(批量)"); 
      // 此处根据是否有搜索信息来进行筛选
      if(!input('?request.')){
        $data=input('request.');
        foreach($data as $k=>$v){
          if($v!=null& in_array($k,['organization_is_sth','organization_sth_num','organization_static'])){     /* 使用in_array($k,['is_sth','sth_num','static'],是因为paginate传参是get方法,需要筛选变量 */
            $k=str_replace("organization_","",$k);
            $this->organization_filter[$k]=$v;
          }
          if($v!=null& in_array($k,['position_is_sth','position_sth_num','position_static'])){     /* 使用in_array($k,['is_sth','sth_num','static'],是因为paginate传参是get方法,需要筛选变量 */
            $k=str_replace("position_","",$k);
            $this->position_filter[$k]=$v;
          }
        }
      }
      // 此处是获取organization清单
      //注:此处使用了scope('static')后,不会显示static=0,即'删除'的数据
      $list=OrganizationModel::scope('static')->where($this->organization_filter)->order('id','desc')->select();   /* 这里用的是查询构建器的select()方法;或者可以使用模型的all()方法,$list=OrganizationModel::all(); */
      $countall=OrganizationModel::scope('static')->where($this->organization_filter)->count();
      $this->assign('organization_list',$list);
      $this->assign('organization_countall',$countall);
      // 此处是获取position清单
      $list=PositionModel::scope('static')->where($this->position_filter)->order('id','desc')->select();   /* 这里用的是查询构建器的select()方法;或者可以使用模型的all()方法,$list=PositionModel::all(); */
      $countall=PositionModel::scope('static')->where($this->position_filter)->count();
      $this->assign('position_list',$list);
      $this->assign('position_countall',$countall);
      return $this->fetch();
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
      $msg=array();
      $data=input('request.');
      if(empty($data['select_organization'])){
        $this->error("请选择组织机构",'create','',1);
      }
      $select_organization_id=$data['select_organization'];
      if(empty($data['select_position'])){
        $this->error("请选择职位工程师",'create','',1);
      }
      $select_position_id=$data['select_position'];
      // dump($select_position);exit;
      foreach($select_organization_id as $emp_id){
        $organization=OrganizationModel::getById($emp_id);
        // 取出$organization已经写入数据库的org_id;
        $org_id_in_database=Db::name('organization_position')->field('position_id')->where('organization_id',$emp_id)->select();
        // 给查询出的数据降维
        foreach($org_id_in_database as $k=>$v){
          $org_id_in_database[$k]=$v['position_id'];
        }
        $select_position_id=array_diff($select_position_id,$org_id_in_database);
        if(empty($select_position_id)){
          $text="组织机构:".$organization->title."所选职务均已分配过";
          array_push($msg,$text);
        }
        foreach($select_position_id as $org_id){
          $position=PositionModel::getById($org_id);
          $organization->position()->attach($position);
          $text="组织机构:".$organization->title."职务:".$position->title."分配完毕";
          array_push($msg,$text);
        }
      }
      dump($msg);
      $this->success("操作完成",'index','',1);
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
      //验证Cookie↓
      $this->checkCookie();
      // 引入navbar
      $this->setNavbar();
      $this->assign('title',"组织机构职务信息");     
      $organization=OrganizationModel::get($id,'position');
      $organization_position=$organization->position;
      // dump( $organization_position);
      $this->assign('organization',$organization);
      $this->assign('organization_position',$organization_position);
      return $this->fetch();
      // return view();
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
      //验证Cookie↓
      $this->checkCookie();
      // 引入navbar
      $this->setNavbar();
      $this->assign('title',"组织机构职务编辑");
      $organization=OrganizationModel::getById($id);
      $organization_position=$organization->position;
      $this->assign('organization',$organization);
      $this->assign('organization_position',$organization_position);
      $countall=PositionModel::scope('static')->count();
      $this->assign('countall',$countall);
      $position_tree=PositionModel::getTree();
      $this->assign('position_tree',$position_tree);
      // dump($position_tree);
      return $this->fetch();
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        dump(input('post.'));
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($emp_id,$org_id)
    {
      $organization=OrganizationModel::getById($emp_id);
      $position=PositionModel::getById($org_id);
      $organization->position()->detach($position);
      $this->redirect('read',['id'=>$emp_id]);
    }
}
